import React, { useState, useEffect } from "react";
import fishTokenContract from "../fishToken";
import web3 from "../web3";

const Balance = () => {
    const [account, setAccount] = useState("");
    const [balance, setBalance] = useState("0");

    useEffect(() => {
        const fetchBalance = async () => {
            const accounts = await web3.eth.getAccounts();
            if (accounts.length === 0) {
                alert("请连接钱包");
                return;
            }
            setAccount(accounts[0]);
            const balance = await fishTokenContract.methods.balanceOf(accounts[0]).call();
            setBalance(balance);
        };
        fetchBalance();
    }, []);

    return (
        <div>
            <h2>账户地址: {account}</h2>
            <h3>鱼币余额: {balance}</h3>
        </div>
    );
};

export default Balance;

